//
//  YSLocationDao.m
//  Yueshi
//
//  Created by Northwan on 15/7/29.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSLocationDao.h"
#import "FMDatabase.h"
#import "YSAddressDBModel.h"

#define TABLE_NAME @"delivery"

@interface YSLocationDao()
{
    FMDatabase *db;
}
@end

@implementation YSLocationDao

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString *database_path = [[NSBundle mainBundle] pathForResource:@"location" ofType:@"db"];;
        db = [FMDatabase databaseWithPath:database_path];
    }
    return self;
}

/**
 * 得到省份的array
 *
 * @return
 */
-(NSArray *)getProvinces{
    NSMutableArray *array = [NSMutableArray array];
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE level = '1'", TABLE_NAME];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * name = [rs stringForColumn:@"name"];
            int ID = [rs intForColumn:@"id"];
            YSAddressDBModel *bean = [[YSAddressDBModel alloc]init];
            [bean setProvinceId:ID];
            [bean setProvinceName:name];
            [array addObject:bean];
        }
        [db close];
        return [NSArray arrayWithArray:array];
    }
    return array;
}

/**
 * 得到城市的Array
 *
 * @return
 */
-(NSArray *)getCitys:(int)provinceId{
    NSMutableArray *array = [NSMutableArray array];
    if([db open]){
        NSString *sql = @"select * from delivery where id like '##%' and level = '2'";
        sql =  [sql stringByReplacingOccurrencesOfString:@"##" withString:[NSString stringWithFormat:@"%d",provinceId]];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * name = [rs stringForColumn:@"name"];
            int ID = [rs intForColumn:@"id"];
            YSAddressDBModel *bean = [[YSAddressDBModel alloc]init];
            [bean setCityId:ID];
            [bean setCityName:name];
            [array addObject:bean];
        }
        [db close];
        return [NSArray arrayWithArray:array];
    }
    return array;
}

/**
 * 得到县的array
 *
 * @return
 */
-(NSArray *)getAreas:(int)cityId{
    NSMutableArray *array = [NSMutableArray array];
    if([db open]){
        
       
        NSString *sql = @"select * from delivery where  id like '##%' and `level`=3 and name <>''";
        sql =  [sql stringByReplacingOccurrencesOfString:@"##" withString:[NSString stringWithFormat:@"%d",cityId]];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * name = [rs stringForColumn:@"name"];
            NSString *ID = [rs stringForColumn:@"id"];
            YSAddressDBModel *bean = [[YSAddressDBModel alloc]init];
            [bean setAreaId:ID];
            [bean setAreaName:name];
            [array addObject:bean];
        }
        [db close];
        return [NSArray arrayWithArray:array];
    }
    return array;
}


/**
 * 查询指定id的省级名称
 *
 * @param provinceId 省级id
 * @return
 */
-(NSString *)getProvinceName:(int)provinceId{
    NSString *result = @"";
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT name FROM %@ WHERE Id = '%d'",TABLE_NAME,provinceId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumnIndex:0];
        }
        [db close];
        return result;
    }
    return result;
}

/**
 * 查询指定id的市级名称
 *
 * @param cityId 市级id
 * @return
 */
-(NSString *)getCityName:(int)cityId{
    NSString *result = @"";
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT name FROM %@ WHERE id = '%d'",TABLE_NAME,cityId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumnIndex:0];
        }
        [db close];
        return result;
    }
    return result;
}

/**
 * 查询指定id的县级名称
 *
 * @param areaId 县级id
 * @return
 */
-(NSString *)getAreaName:(NSString *)areaId{
    NSString *result = @"";
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT name FROM %@ WHERE id = '%@'",TABLE_NAME,areaId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumnIndex:0];
        }
        [db close];
        return result;
    }
    return result;
}

/**
 * 获取省市县组合的名字
 *
 * @param provinceId
 * @param cityId
 * @param areaId
 * @param spliteStr  分割线
 * @return
 */
-(NSString *)getDetaild:(int)provinceId :(int)cityId :(NSString *)areaId{
    NSMutableString *result = [NSMutableString string];
    [result appendString:[self getProvinceName:provinceId]];
    [result appendString:[self getCityName:cityId]];
    [result appendString:[self getAreaName:areaId]];
    return result;
}


-(int)getProvinceId:(NSString *)province{
    int result;
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"select * from delivery  where name = '%@' and level = '1'",province];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs intForColumn:@"id"];
        }
        [db close];
        return result;
    }
    return result;
}


-(int)getCityId:(int)provinceId cityName:(NSString *)cityName{
    int result;
    if([db open]){
        NSString *sql = @"select * from delivery where id like '$$%' and name='##' and level = '2'";
        sql =  [sql stringByReplacingOccurrencesOfString:@"##" withString:[NSString stringWithFormat:@"%@",cityName]];
        sql =  [sql stringByReplacingOccurrencesOfString:@"$$" withString:[NSString stringWithFormat:@"%d",provinceId]];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs intForColumn:@"id"];
        }
        [db close];
        return result;
    }
    return result;
}

-(NSString *)getAreaId:(int)cityId areaName:(NSString *)areaName{
    NSString *result = @"";
    if([db open]){
        NSString *sql = @"select * from delivery where id like '$$%' and name='##' and level = '3'";
        sql =  [sql stringByReplacingOccurrencesOfString:@"##" withString:[NSString stringWithFormat:@"%@",areaName]];
        sql =  [sql stringByReplacingOccurrencesOfString:@"$$" withString:[NSString stringWithFormat:@"%d",cityId]];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumn:@"id"];
        }
        [db close];
        return result;
    }
    return result;
}


@end
